/*
Copyright 2020 The Fiberhome Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package main

import (
	"container/list"
	"fmt"
)

type AIPlayer struct {
	ch chan int

	stop chan int

	player *Player
	game   *Game

	bfsDistance [][]int
}

func NewAI(player *Player, game *Game) *AIPlayer {
	return &AIPlayer{
		ch:     make(chan int),
		stop:   make(chan int),
		player: player,
		game:   game,
	}
}

func (ai AIPlayer) Run() {
	for {
		select {
		case <-ai.ch:
			ai.Do()

		case <-ai.stop:
			fmt.Printf("ai stop for %s\n", ai.player.Name)
			return

		}
	}

}

const (
	unreachable = 20000
	inf         = 10000
	body        = 20000
)

var Direction = [][]int{{0, 1}, {0, -1}, {1, 0}, {-1, 0}}

func (ai AIPlayer) fillbfsDistance(x, y int, dis int) bool {
	if x >= len(ai.bfsDistance) || y >= len(ai.bfsDistance[0]) || x < 0 || y < 0 {
		return false
	}
	ai.bfsDistance[x][y] = dis
	return true
}
func (ai AIPlayer) initbfsDistance() {
	ai.bfsDistance = make([][]int, ai.game.WorldWidth())
	for x := range ai.bfsDistance {
		ai.bfsDistance[x] = make([]int, ai.game.WorldHeight())
		for y := range ai.bfsDistance[x] {
			ai.bfsDistance[x][y] = inf
		}
	}
}

func (ai AIPlayer) getbfsDistance(v *Egg) {
	//计算各点到蛋的距离
	ai.initbfsDistance()

	for player := range ai.game.players() {
		ai.fillbfsDistance(player.Pos.RoundX(), player.Pos.RoundY(), body)
		for _, segment := range player.Trail {
			ai.fillbfsDistance(segment.Pos.RoundX(), segment.Pos.RoundY(), body)
		}
	}

	ai.bfsDistance[v.Pos.RoundX()][v.Pos.RoundY()] = 0

	queue := list.New()

	queue.PushBack(v.Pos)
	for queue.Len() > 0 {
		p := queue.Front()
		queue.Remove(p)
		pv := p.Value.(*Position)

		for i, _ := range Direction {
			pt := &Position{
				X: pv.X + float64(Direction[i][0]),
				Y: pv.Y + float64(Direction[i][1]),
			}
			if pt.RoundX() >= len(ai.bfsDistance) || pt.RoundY() >= len(ai.bfsDistance[0]) || pt.RoundX() < 0 || pt.RoundY() < 0 {
				continue
			}
			if inf == ai.bfsDistance[pt.RoundX()][pt.RoundY()] {
				ai.bfsDistance[pt.RoundX()][pt.RoundY()] = ai.bfsDistance[pv.RoundX()][pv.RoundY()] + 1
				queue.PushBack(pt)
			}
		}

	}
}

func (ai AIPlayer) isReachable() bool {
	for i, _ := range Direction {
		pt := &Position{
			X: ai.player.Pos.X + float64(Direction[i][0]),
			Y: ai.player.Pos.Y + float64(Direction[i][1]),
		}
		if ai.bfsDistance[pt.RoundX()][pt.RoundY()] < inf {
			return true
		}
	}
	return false
}

func (ai AIPlayer) Do() {
	print(ai.player.Name)

	for _, v := range ai.game.eggSite {

		ai.getbfsDistance(v)
		if ai.isReachable(){

		}

		//
	}

}
